//
//  HFYBaseDataSource.m
//  huiFengYun
//
//  Created by 王晨旭 on 14/12/21.
//  Copyright (c) 2014年 zm. All rights reserved.
//

#import <CommonCrypto/CommonDigest.h>
#import "HFYBaseDataSource.h"
#import <JSONKit.h>
#import <UIKit/UIKit.h>
#import "HFYJSONResponseSerializer.h"
#import "NSMutableDictionary+Parameter.h"

#define apicall_key @"1234567890ABCDEFFENGHUOHUIFENGYUN"

#define API_URL @"http://tapi.clejw.com/mobile/interface.do"
//#define API_URL @"http://api.clejw.com/mobile/interface.do" //外网
@implementation HFYBaseDataSource

-(Class)responseSerializer
{
    return [HFYJSONResponseSerializer class];
}
//
//-(void)msg:(NSString *)msgId parameters:(NSDictionary *)parameters requestIdentifier:(RequestIdentifier)identifier success:(void (^)(HFYResult *))success
//{
//    NSMutableDictionary *urlpartemp = [NSMutableDictionary new];
//    NSMutableDictionary *urlpar = [NSMutableDictionary new];
//
//    
//    NSString *msg = [parameters JSONString];
//    [urlpartemp setParameter:msg forKey:@"msg"];
//    
//    NSString *uuid = [[UIDevice currentDevice]identifierForVendor].UUIDString;
//    [urlpartemp setObject:uuid forKey:@"uuid"];
//    
//    NSString *ostype = @"ios";
//    [urlpartemp setObject:ostype forKey:@"ostype"];
//    [urlpartemp setObject:msgId forKey:@"msgid"];
//    
//    NSString *signature = [self MD5Digest:[NSString stringWithFormat:@"%@&%@&%@&%@&%@",uuid,ostype,msgId,msg,[self MD5Digest:apicall_key]]];
//    [urlpartemp setObject:signature forKey:@"signature"];
//    
//    NSString *parame = [urlpartemp JSONString];
//    NSLog(@"%@",parame);
//    [urlpar setObject:parame forKey:@"text"];
//    [self post:API_URL parameters:urlpar requestIdentifier:identifier success:success];
// 
//    
//}

-(void)msg:(NSString *)msgId parameters:(NSDictionary *)parameters requestIdentifier:(RequestIdentifier)identifier success:(void (^)(id))success fail:(void (^)(id))fail
{
     NSMutableDictionary *content = [NSMutableDictionary new];
    [content setObject:msgId forKey:@"type"];
    [content addEntriesFromDictionary:parameters];
    [self get:API_URL parameters:@{@"content":[content JSONString]} requestIdentifier:identifier success:success fail:fail];
    
}

-(void)msg:(NSString *)msgId parameters:(NSDictionary *)parameters requestIdentifier:(RequestIdentifier)identifier success:(void (^)(id))success
{
    NSMutableDictionary *content = [NSMutableDictionary new];
    [content setObject:msgId forKey:@"type"];
    [content addEntriesFromDictionary:parameters];
    [self get:API_URL parameters:@{@"content":[content JSONString]} requestIdentifier:identifier success:success fail:nil];
    
}


- (NSString *)MD5Digest:(NSString *)someStr
{
    const char* input = [someStr UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(input, (CC_LONG)strlen(input), result);
    
    NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [digest appendFormat:@"%02x", result[i]];
    }
    
    return digest;
}

-(void)analyzeError:(NSError *)error withRequestIdentifier:(RequestIdentifier)identifier
{
    
    if ([error.domain isEqualToString:TwokeNetworkingErrorDomain]) {
        switch (error.code) {
            case TwokeBadRequestErrorCode:
                [self.requestor networkingFailureHandleWithRequestIdentifier:identifier errorMessage:@"请求错误"];
                break;
                
            case TwokeUserInvalidErrorCode:
                [self.requestor networkingFailureHandleWithRequestIdentifier:identifier errorMessage:@"用户身份验证信息错误"];
                break;
                
            case TwokeRequestInvalidErrorCode:
                [self.requestor networkingFailureHandleWithRequestIdentifier:identifier errorMessage:@"接口失效"];
                break;
                
            case TwokeNotFoundErrorCode:
                [self.requestor networkingFailureHandleWithRequestIdentifier:identifier errorMessage:@"请求资源未找到"];
                break;
                
            case TwokePermissionDeniedErrorCode:
                [self.requestor networkingFailureHandleWithRequestIdentifier:identifier errorMessage:@"您无权访问该资源"];
                break;
                
            case TwokeBadServerErrorCode:
                [self.requestor networkingFailureHandleWithRequestIdentifier:identifier errorMessage:error.userInfo[@"msg"]];
                break;
            case TwokeUnkownDataErrorCode:
                [self.requestor networkingFailureHandleWithRequestIdentifier:identifier errorMessage:@"未知数据"];
                break;
            default:
                [self.requestor networkingFailureHandleWithRequestIdentifier:identifier errorMessage:error.userInfo[@"msg"]];
                break;
        }
    }else if([error.domain isEqualToString:JSONModelErrorDomain]){
        if ([self.requestor respondsToSelector:@selector(networkingFailureHandleWithRequestIdentifier:errorMessage:)]) {
            [self.requestor networkingFailureHandleWithRequestIdentifier:identifier errorMessage:InvalidDataError];
        }
        
    }else{
        NSLog(@"%@",error);
        if ([self.requestor respondsToSelector:@selector(networkingFailureHandleWithRequestIdentifier:errorMessage:)]) {
            [self.requestor networkingFailureHandleWithRequestIdentifier:identifier errorMessage:NetworkingError];
        }
    }
    if ([self.requestor respondsToSelector:@selector(networkingCompleteHandleWithRequestIdentifier:)]) {
        [self.requestor networkingCompleteHandleWithRequestIdentifier:identifier];
    }
    
}

@end
